﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Web;
using Ha666.Common;
using Top.Api.Util;

namespace Ha666.APIClient
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var client = new HttpClient())
            {
                while (true)
                {
                    int count = 0;
                    Stopwatch timer = new Stopwatch();
                    timer.Start();
                    for (int i = 0; i < 1000; i++)
                    {
                        try
                        {
                            Dictionary<string, string> dictionary = new Dictionary<string, string>();
                            dictionary.Add("Appkey", APIServices.API_APP_KEY);
                            dictionary.Add("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                            dictionary.Add("v", "1.0");
                            //dictionary.Add("tid", (10001 + i).ToString());
                            dictionary.Add("token", Guid.NewGuid().ToString().Replace("-", "").ToUpper());
                            dictionary.Add("sign", TopUtils.SignTopRequest(dictionary, APIServices.API_APP_SECRET));
                            HttpResponseMessage message =
                                client.PostAsync(APIServices.API_APP_URL + "System.GetTime", new FormUrlEncodedContent(dictionary))
                                    .Result;
                            string ReasonPhrase = HttpUtility.UrlDecode(message.ReasonPhrase);
                            byte[] Result = message.Content.ReadAsByteArrayAsync().Result;
                            ServerTime time = ProtobufHelper.GetObject<ServerTime>(Result);
                            var code = message.StatusCode;
                            if (code == HttpStatusCode.OK)
                                count++;
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine("{0}\t出错：{1}", DateTime.Now.ToString("HH:mm:ss.fff"), exception.Message);
                        }
                    }
                    //Clear();
                    timer.Stop();
                    Console.WriteLine("{0}\t共操作{1}条记录\t耗时{2}毫秒。", DateTime.Now.ToString("HH:mm:ss.fff"), count, timer.ElapsedMilliseconds);
                }
            }
        }

        static void Clear()
        {
            using (var client = new HttpClient())
            {
                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                dictionary.Add("Appkey", APIServices.API_APP_KEY);
                dictionary.Add("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                dictionary.Add("v", "1.0");
                dictionary.Add("token", Guid.NewGuid().ToString().Replace("-", "").ToUpper());
                dictionary.Add("sign", TopUtils.SignTopRequest(dictionary, APIServices.API_APP_SECRET));
                HttpResponseMessage message =
                    client.PostAsync(APIServices.API_APP_URL + "Trade.ClearCache", new FormUrlEncodedContent(dictionary))
                        .Result;
                string ReasonPhrase = HttpUtility.UrlDecode(message.ReasonPhrase);
                string Result = message.Content.ReadAsStringAsync().Result;
                var code = message.StatusCode;
            }
        }
    }
}
